package lava.rt.wrapper;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import java.util.function.BiFunction;


public class FileWrapper extends SelfWrapper<File>{

	
	
	public FileWrapper(File self) throws IOException {
		super(self);
		if(self.exists()&&!self.isFile()) {
			throw new IOException(self+" is not a file");
		}
	}

	
	
   
    
     private final static String CSV_SPLIT=",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
	
	public   List<String[]> readCsv() throws Exception {
		List<String[]> rets=new ArrayList<>();
		List<String> lines=readLines();
		for(String line: lines) {
			String[] row=line.trim().split(CSV_SPLIT, -1);
			rets.add(row);
		}
		
		return rets;
	}
    
	
	    
	    
	    public  String readContent() throws  IOException{
			 StringBuffer ret=new StringBuffer();
			   
			   try(    
					   FileReader fileReader=new FileReader(self);
					   BufferedReader reader = new BufferedReader(fileReader);){
	          
	         String line = null;  
	         
	         while((line=reader.readLine())!=null){  
	        
	             ret.append(line);
	             
	          }
			 }
			   
			 return ret.toString();
		}
	    
	    public  List<String> readLines() throws  IOException{
			 List<String> ret=new ArrayList<>();
			  
			   try(    
					   FileReader fileReader=new FileReader(self);
					   BufferedReader reader = new BufferedReader(fileReader);){
	          
	         String line = null;  
	         while((line=reader.readLine())!=null){  
	        
	             ret.add(line);  
	             
	          }
			   }
			   
			   return ret;
		}
		
		
		public void readLines(BiFunction<Integer,String,Integer> handler) throws FileNotFoundException, IOException{
			 
			   
			   try(    
					   FileReader fileReader=new FileReader(self);
					   BufferedReader reader = new BufferedReader(fileReader);){//换成你的文件名 
	         
	        String line = null;  
	        int lineIndex=0,lineStep=0;
	        while((line=reader.readLine())!=null&&lineStep>=0){  
	               if(lineStep>0) {
	            	   lineStep--;
	            	   continue;
	               }
	               lineStep=handler.apply(lineIndex,line);
	            
	         }
			}
			   
			   
		}
	    
		@SuppressWarnings("unchecked")
		public <T> T readObject() {
			T ret=null;
			try(    FileInputStream fis=new FileInputStream(self);
					ObjectInputStream ois=new ObjectInputStream(fis); ){
				ret=(T)ois.readObject();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return ret; 
		}
		
		public void writeObject(Object obj) {
			
			try(    FileOutputStream fos=new FileOutputStream(self);
					ObjectOutputStream ois=new ObjectOutputStream(fos); ){
			       ois.writeObject(obj);	
			} catch (IOException e) {
				
				e.printStackTrace();
			} 
			 
		}
		
		public int download(URL url) throws IOException {
			int bytesum = 0;
			URLConnection conn = url.openConnection();
			try(InputStream is = conn.getInputStream();
					FileOutputStream fos = new FileOutputStream(self);
					){
				byte[] buffer = new byte[1024];
				int byteread;
			    while ((byteread = is.read(buffer)) != -1) {
			    	    bytesum += byteread;
				        fos.write(buffer, 0, byteread);
				}
			}
			return bytesum;
		}
		
		public int upload(URL url) throws IOException {
			int bytesum = 0;
			URLConnection conn = url.openConnection();
			try(OutputStream os = conn.getOutputStream();
					FileInputStream fis = new FileInputStream(self);
					){
				byte[] buffer = new byte[1024];
				int byteread;
			    while ((byteread = fis.read(buffer)) != -1) {
			    	    bytesum += byteread;
				        os.write(buffer, 0, byteread);
				}
			}
			return bytesum;
		}
	
}
